You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

[...nextauth].ts 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { NextApiRequest, NextApiResponse } from 'next'
  2. import NextAuth from 'next-auth'
  3. import Providers from 'next-auth/providers'
  4. export default function (req: NextApiRequest, res: NextApiResponse) {
  5. return NextAuth(req, res, {
  6. providers: [
  7. Providers.GitHub({
  8. clientId: process.env.GITHUB_ID,
  9. clientSecret: process.env.GITHUB_SECRET,
  10. scope: 'user',
  11. }),
  12. ],
  13. callbacks: {
  14. async redirect(url, baseUrl) {
  15. return url.startsWith(baseUrl) ? url : baseUrl
  16. },
  17. async signIn(user, account, profile) {
  18. // @ts-ignore
  19. const canLogin = await isSponsoringMe(profile?.login)
  20. if (canLogin) {
  21. return canLogin
  22. } else {
  23. return '/sponsorware'
  24. }
  25. },
  26. },
  27. })
  28. }
  29. const whitelist = ['steveruizok']
  30. async function isSponsoringMe(login: string) {
  31. if (whitelist.includes(login)) return true
  32. const res = await fetch('https://api.github.com/graphql', {
  33. method: 'POST',
  34. headers: {
  35. 'Content-Type': 'application/json',
  36. Authorization: 'bearer ' + process.env.GITHUB_API_SECRET,
  37. },
  38. body: JSON.stringify({
  39. query: `
  40. query {
  41. user(login: "steveruizok") {
  42. isSponsoredBy(accountLogin: "${login}")
  43. }
  44. }
  45. `,
  46. }),
  47. }).then((res) => res.json())
  48. return res?.data?.user?.isSponsoredBy
  49. }